//
//  AppDelegate.m
//  JQFY
//
//  Created by apple on 2017/8/6.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "AppDelegate.h"
#import "JQFYBaseNavigationController.h"
#import "JQFYTabbarController.h"
#import "JQFYLonginViewController.h"
#import "JQFYPlayList.h"
#import "JQFYMusicPlayer.h"
#import "JQFYPlayListManger.h"
#import "JQFYTicketInfo_ViewController.h"
#import "JQFYUMShare.h"
#import "JQFYSettingUserInfoViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"

@interface AppDelegate () <WXApiDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 像微信注册用户
    [WXApi registerApp:@"wx1173e230d93a4c40"];
    //启动页停留1秒
    [NSThread sleepForTimeInterval:1];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
//    JQFYUserData *userData = [JQFYUserData userDataInSandbox];
//    JQFYTabbarController *tbVC = [[JQFYTabbarController alloc]init];
//    self.window.rootViewController =tbVC;
//    JQFYLonginViewController *logVc =[[JQFYLonginViewController alloc]init];
//    self.window.rootViewController =logVc;
    
//    if (userData.isLogin) {
//        JQFYTabbarController *tbVC = [[JQFYTabbarController alloc]init];
//        self.window.rootViewController =tbVC;
//    }else{
//        JQFYLonginViewController *logVc =[[JQFYLonginViewController alloc]init];
//        self.window.rootViewController =logVc;
//    }
    
    JQFYTabbarController *tbVC = [[JQFYTabbarController alloc]init];
    self.window.rootViewController =tbVC;
    [self.window makeKeyAndVisible];
    //检查网络连接
    [[JQFYNetworkTool sharedManager] isReachableByAFN];
    [[UIApplication sharedApplication] becomeFirstResponder];
    // 开启接受远程事件
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [[JQFYUMShare new] installUMShare];
    
    //初始化环信
//    EMOptions *options = [EMOptions optionsWithAppkey:@"1156170809178235#jiqifanyu"];//appkey   1156170915115507#uninhibited
//    options.apnsCertName = @"uninhibited";//推送证书  uninhibited_dev
//    EMError *error = [[EMClient sharedClient] initializeSDKWithOptions:options];
//    if (!error)
//    {
//         NSLog(@"初始化成功");
//    }
    //初始化
//    [[EaseSDKHelper shareHelper] hyphenateApplication:application
//                        didFinishLaunchingWithOptions:launchOptions
//                                               appkey:@"1156170809178235#jiqifanyu"
//                                         apnsCertName:@"uninhibited"
//                                          otherConfig:@{kSDKConfigEnableConsoleLogger:[NSNumber numberWithBool:YES]}];
    return YES;
}
- (BOOL)becomeFirstResponder
{
    return YES;
}
/// 锁屏后处理播放的事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl) {
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlPlay:
                [__Player__ play];
                break;
            case UIEventSubtypeRemoteControlPause:
                [__Player__ pause];
                break;
            case UIEventSubtypeRemoteControlStop:
                [__Player__ pause];
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                [__Player__ next];
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                [__Player__ prev];
                break;
        }
    }
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
        if (result) {
            // 其他如支付等SDK的回调
            return result;
        }
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];
        return YES;
    }
    return  [WXApi handleOpenURL:url delegate:self];
}

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    BOOL result = [[UMSocialManager defaultManager]  handleOpenURL:url options:options];
    if (result) {
        // 其他如支付等SDK的回调
        return result;
    }
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];
        return YES;
    }
    return  [WXApi handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
        // 其他如支付等SDK的回调
        return [WXApi handleOpenURL:url delegate:self];
    }
    return result;
}

#pragma mark - 微信支付完成回调 -
-(void) onResp:(BaseResp*)resp
{
    //启动微信支付的response
    NSString *payResoult = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
    if([resp isKindOfClass:[PayResp class]]){
        //支付返回结果，实际支付结果需要去微信服务器端查询
        switch (resp.errCode) {
            case 0:
                payResoult = @"支付结果：成功！";
                [[NSNotificationCenter defaultCenter] postNotificationName:@"paySucess" object:nil];
                break;
            case -1:
                payResoult = @"支付结果：失败！";
                break;
            case -2:
                payResoult = @"用户已经退出支付！";
                break;
            default:
                payResoult = [NSString stringWithFormat:@"支付结果：失败！retcode = %d, retstr = %@", resp.errCode,resp.errStr];
                break;
        }
    }
}

#pragma mark -
#pragma mark 应用程序将要退出

-(void)applicationWillTerminate:(UIApplication *)application{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"isSave"];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"appBecomeActive" object:nil];
}

////进入后台
//-(void)applicationDidEnterBackground:(UIApplication *)application{
//    [[EMClient sharedClient] applicationDidEnterBackground:application];
//}
////从后台返回
//-(void)applicationWillEnterForeground:(UIApplication *)application{
//    [[EMClient sharedClient] applicationWillEnterForeground:application];
//}

@end
